package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IMissionItemFeedbackDAO;
import org.jsola.hr.dto.MissionCenterFeedBackDTO;
import org.jsola.hr.dto.MissionItemFeedbackAddDTO;
import org.jsola.hr.dto.MissionItemFeedbackUpdateDTO;
import org.jsola.hr.entity.MissionItemFeedbackDO;
import org.jsola.hr.query.MissionItemFeedbackQuery;
import org.jsola.hr.service.IMissionItemFeedbackService;
import org.jsola.hr.vo.MissionItemFeedbackListVO;
import org.jsola.hr.vo.MissionItemFeedbackVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务调度中心子项反馈记录
 *
 * @author zhr
 */
@Service("hrMissionItemFeedbackServiceImpl")
public class MissionItemFeedbackServiceImpl implements IMissionItemFeedbackService {

    @Autowired
    private IMissionItemFeedbackDAO missionItemFeedbackDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public MissionItemFeedbackVO save(MissionItemFeedbackAddDTO missionItemFeedbackAddDTO, TokenUser tokenUser) {
        // 转DO
        MissionItemFeedbackDO missionItemFeedbackDO = missionItemFeedbackAddDTO.to(MissionItemFeedbackDO.class);
        // 保存
        missionItemFeedbackDO = save(missionItemFeedbackDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return missionItemFeedbackDO.to(MissionItemFeedbackVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(MissionItemFeedbackUpdateDTO missionItemFeedbackUpdateDTO, TokenUser tokenUser) {
        //转DO
        MissionItemFeedbackDO missionItemFeedbackDO = missionItemFeedbackUpdateDTO.to(MissionItemFeedbackDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(missionItemFeedbackDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... missionItemFeedbackIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) missionItemFeedbackIds);
    }


    @Override
    public MissionItemFeedbackVO selectById(String missionItemFeedbackId, String siteId) {
        MissionItemFeedbackDO missionItemFeedbackDO = selectDOById(missionItemFeedbackId, siteId);
        if (missionItemFeedbackDO == null) {
            return null;
        }
        return missionItemFeedbackDO.to(MissionItemFeedbackVO.class);
    }

    @Override
    public List<MissionItemFeedbackListVO> select(MissionItemFeedbackQuery missionItemFeedbackQuery, String siteId) {
        List<MissionItemFeedbackDO> missionItemFeedbackDOList = selectDO(missionItemFeedbackQuery, siteId);
        if (CollectionUtils.isEmpty(missionItemFeedbackDOList)) {
            return missionItemFeedbackDOList == null ? null : new ArrayList<>();
        }
        return missionItemFeedbackDOList.stream()
                .map(missionItemFeedbackDO -> missionItemFeedbackDO.to(MissionItemFeedbackListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(MissionItemFeedbackQuery missionItemFeedbackQuery, String siteId) {
        Example example = buildExample(missionItemFeedbackQuery, siteId);
        return missionItemFeedbackDAO.selectCountByExample(example);
    }

    @Override
    public Page<MissionItemFeedbackListVO> selectPage(MissionItemFeedbackQuery missionItemFeedbackQuery, String siteId) {
        Example example = buildExample(missionItemFeedbackQuery, siteId);
        Page<MissionItemFeedbackDO> page = missionItemFeedbackDAO.selectPageByExample(example,
                missionItemFeedbackQuery.getPageNo(),
                missionItemFeedbackQuery.getPageSize());

        return page.to(MissionItemFeedbackListVO.class);
    }

    @Override
    public MissionItemFeedbackDO selectDOById(String missionItemFeedbackId, String siteId) {
        return listById(missionItemFeedbackId, siteId);
    }

    @Override
    public List<MissionItemFeedbackDO> selectDO(MissionItemFeedbackQuery missionItemFeedbackQuery, String siteId) {
        Example example = buildExample(missionItemFeedbackQuery, siteId);
        return missionItemFeedbackDAO.selectByExample(example);
    }

    @Override
    public List<MissionItemFeedbackListVO> selectFeedbackByItemId(String itemId) {
        return missionItemFeedbackDAO.selectFeedbackByItemId(itemId);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void batchSaveFeedback(MissionCenterFeedBackDTO feedBackDTO, TokenUser tokenUser) {
        List<MissionItemFeedbackDO> batchSaveInsertList = new ArrayList<>();
        List<String> itemIdList = feedBackDTO.getItemIdList();
        for (String itemId : itemIdList) {
            MissionItemFeedbackDO insert = new MissionItemFeedbackDO();
            insert.setState(feedBackDTO.getState());
            insert.setFeedbackContent(feedBackDTO.getRemark());
            insert.setMissionItemId(itemId);
            insert.setSiteId(tokenUser.getSiteId());
            insert.preInsert(tokenUser.getUserId());
            batchSaveInsertList.add(insert);
        }
        missionItemFeedbackDAO.insertListAndSetId(batchSaveInsertList);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param missionItemFeedbackQuery 查询参数
     * @param siteId                   所属站点id
     * @return example
     */
    private Example buildExample(MissionItemFeedbackQuery missionItemFeedbackQuery, String siteId) {
        Example example = new Example(MissionItemFeedbackDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, missionItemFeedbackQuery.getOrders());
        return example;
    }
}




